package com.imooc.miaosha.controller;

import com.imooc.miaosha.exception.GlobalException;
import com.imooc.miaosha.form.LoginForm;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.result.ResultEnums;
import com.imooc.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 模块描述: <br>
 * (登录模块控制层)
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/16 23:02
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping("/to_login")
    public String toLogin() {
        return "login";
    }

    /**
     * 使用注解（@Valid）校验的方式，替代if/else的校验方式
     * @param loginForm
     * @return
     */
    @RequestMapping("/do_login")
    @ResponseBody
    public Result<Boolean> doLogin(@Valid LoginForm loginForm) {
        if (loginForm == null) {
            throw new GlobalException(ResultEnums.PARAM_OBJ_EMPTY);
        }

        ResultEnums resultEnums = userService.doLogin(loginForm);
        return Result.success(resultEnums);
    }

    @Autowired
    private UserService userService;

}
